home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / GXEdit Library & Doc / GXEditValidation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  2.5 KB  |  144 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:            GXEditValidation.c
  3.     
  4.     Contains:
  5.     
  6.     Written by:        Barton R. House
  7.     
  8.     Copyright:        © 1993 by Apple Computer, Inc., All rights reserved.
  9.     
  10. */
  11.  
  12. #include <Memory.h>
  13.  
  14. #include "GXEdit.h"
  15. #include "GXEditDoc.h"
  16. #include "GXEditValidation.h"
  17. #include "GXEditError.h"
  18. #include "GXEditNewRun.h"
  19. #include "GXEditDebug.h"
  20.  
  21. #define gxEditFullValidation    GXEditDebug
  22.  
  23. static Boolean            CheckMagic(DocPtr dp);
  24. static Boolean            CheckDoc(DocPtr dp);
  25. static Boolean            CheckParagraph(DocPtr dp, ParaPtr pp);
  26. static Boolean            CheckRun(DocPtr dp, NewRunPtr rp);
  27. static Boolean             CheckRefCount(DocPtr dp);
  28.  
  29. Boolean ValidateDoc(DocPtr dp)
  30. {
  31.     return(CheckDoc(dp));
  32. }
  33.  
  34. static Boolean CheckMagic(DocPtr dp)
  35. {
  36.     if(dp->magic != kGXEditMagic)
  37.         gxEditPostError(nil, gx_edit_internal_fatal_error);
  38.         
  39.     return(true);
  40. }
  41.  
  42. static Boolean CheckRun(DocPtr dp, NewRunPtr rp)
  43. {
  44.     if(rp->styleIndex < 0 || rp->styleIndex >= dp->numStyles) {
  45.         gxEditPostError(nil, gx_edit_internal_fatal_error);
  46.         return(false);
  47.     }
  48.     
  49.     if(rp->numText == 0)
  50.         gxEditPostError(dp, gx_edit_warning);
  51.         
  52.     return(true);
  53. }
  54.  
  55. static Boolean CheckParagraph(DocPtr dp, ParaPtr pp)
  56. {
  57.     int            runIndex;
  58.     NewRunPtr    rp;
  59.     Boolean        ok = true;
  60.     
  61.     HLock((Handle) pp->runs);
  62.     rp = *pp->runs;
  63.     
  64.     for(runIndex = 0; runIndex < pp->numRuns; runIndex++, rp++) {
  65.     
  66.         if(!CheckRun(dp, rp)) {
  67.             ok = false;
  68.             break;
  69.         }
  70.  
  71.         if(runIndex != 0 && (rp->styleIndex == (rp-1)->styleIndex))
  72.             gxEditPostError(dp, gx_edit_warning);
  73.  
  74.     }
  75.         
  76.     HUnlock((Handle) pp->runs);
  77.  
  78.     return(ok);
  79. }
  80.  
  81. static Boolean CheckRefCount(DocPtr dp)
  82. {
  83.     ParaPtr        pp;
  84.     StylePtr        sp;
  85.     long            runCount;
  86.     long            styleCount;
  87.     long            scrapCount;
  88.     short        paraIndex;
  89.     short        styleIndex;
  90.     
  91.     runCount = 0;
  92.     styleCount = 0;
  93.     
  94.     scrapCount = dp->scrap.numRuns;
  95.     
  96.     pp = *dp->paragraphs;
  97.     
  98.     for(paraIndex = 0; paraIndex < dp->numParagraphs; paraIndex++, pp++)
  99.         runCount += pp->numRuns;
  100.     
  101.     sp = *dp->styles;
  102.     
  103.     for(styleIndex = 0; styleIndex < dp->numStyles; styleIndex++, sp++)
  104.         styleCount += sp->refCount;
  105.         
  106.     if(styleCount != (runCount + scrapCount)) {
  107.         gxEditPostError(nil, gx_edit_internal_fatal_error);
  108.         return(false);
  109.     }
  110.         
  111.     return(true);
  112.     
  113. }
  114.  
  115. static Boolean CheckDoc(DocPtr dp)
  116. {
  117.     Boolean    ok = true;
  118.     
  119.     /* for now, just check the magic number */
  120.     
  121.     if(!CheckMagic(dp))
  122.         return(false);
  123.  
  124. #if gxEditFullValidation
  125.  
  126.     HLock((Handle) dp->paragraphs);
  127.     
  128.     pp = *dp->paragraphs;
  129.     
  130.     for(paraIndex = 0; paraIndex < dp->numParagraphs; paraIndex++, pp++)
  131.         if(!CheckParagraph(dp, pp)) {
  132.             ok = false;
  133.             break;
  134.         }
  135.             
  136.     if(ok) ok = CheckRefCount(dp);
  137.     
  138.     HUnlock((Handle) dp->paragraphs);
  139.     
  140. #endif
  141.         
  142.     return(ok);
  143. }
  144.